package wml.switcher;

import java.io.IOException;

import wml.switcher.command.ActionCommand;
import wml.switcher.tranceiver.CommandSender;
import wml.switcher.view.DromListener;
import wml.switcher.view.MainView;
import wml.switcher.view.MarkListener;
import wml.switcher.view.ViewController;

import com.jiahui.view.ActionItem;
import com.jiahui.view.QuickActionBar;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.LinearLayout;

public class ViewSwitcherActivity extends Activity {

	private boolean nailed;
	private CommandSender commandSender;
	private MarkListener markListener;
	private DromListener dromListener;

	private MainView mainView;
	private Gallery gallery;
	private Button btnNail;
	private ViewController viewController;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.imageswitcherpage);

		MyApplication.getInstance().addActivity(this);
		Intent intent = this.getIntent();// 得到激活它的Intent
		String ip = intent.getStringExtra("ip");

		this.setCommandSender(new CommandSender(ip, 12345));
		this.markListener = new MarkListener(this.getCommandSender());
		this.dromListener = new DromListener();

		this.btnNail = (Button) this.findViewById(R.id.nail);
		this.gallery = (Gallery) findViewById(R.id.gallery);

		try {
			this.viewController = new ViewController(this, "http://" + ip
					+ ":8080/");
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.getGallery().setAdapter(viewController);
		this.getGallery().setOnItemSelectedListener(viewController);

		this.mainView = new MainView(this);
		this.mainView.setViewController(viewController);
		this.mainView.setOnTouchListener(this.dromListener);
		((LinearLayout) this.findViewById(R.id.canvas)).addView(this.mainView);

		this.initQuickBar();
		new Thread(this.getCommandSender()).start();
	}

	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		this.refreshNailed(event);
		if (this.nailed) {
			this.markListener.onMark(this.getMainView(), event);
		} else {
			return super.dispatchTouchEvent(event);
		}
		return true;
	}

	private void refreshNailed(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			float x = event.getX(0);
			float y = event.getY(0);
			this.nailed = inNailButton(x, y);
		} else if (event.getAction() == MotionEvent.ACTION_UP
				|| event.getAction() == MotionEvent.ACTION_POINTER_1_UP) {
			this.nailed = false;
		}
	}

	private boolean inNailButton(float x, float y) {
		int[] location = new int[2];
		this.btnNail.getLocationOnScreen(location);
		if (0 < x && x < this.btnNail.getWidth() && location[1] < y) {
			return true;
		}
		return false;
	}

	private void initQuickBar() {
		final ActionItem action_wml = new ActionItem();
		action_wml.setTitle("@@王明亮制造@@");
		action_wml.setIcon(getResources().getDrawable(
				R.drawable.action_comment));
		
		final ActionItem action_removePre = new ActionItem();
		action_removePre.setTitle("擦除上一条");
		action_removePre.setIcon(getResources().getDrawable(
				R.drawable.action_comment));
		action_removePre.setClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				viewController.removePrePath();
				getCommandSender().sendCommand(
						new ActionCommand(ActionCommand.ACTION_ERS_PRE,
								ViewController.getCurrentPosition()));
			}
		});

		final ActionItem action_exit = new ActionItem();
		action_exit.setTitle("退出");
		action_exit.setIcon(getResources().getDrawable(
				R.drawable.action_comment));
		action_exit.setClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				MyApplication.getInstance().exit();
			}
		});

		final ActionItem action_removeAll = new ActionItem();
		action_removeAll.setTitle("擦除所有");
		action_removeAll.setIcon(getResources().getDrawable(
				R.drawable.action_comment));
		action_removeAll.setClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				viewController.removeAllPaths();
				getCommandSender().sendCommand(
						new ActionCommand(ActionCommand.ACTION_ERS_ALL,
								ViewController.getCurrentPosition()));
			}
		});

		((Button) this.findViewById(R.id.btn_click))
				.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						QuickActionBar actionBar = new QuickActionBar(v);
						actionBar.addActionItem(action_wml);
						actionBar.addActionItem(action_removeAll);
						actionBar.addActionItem(action_removePre);
						actionBar.addActionItem(action_exit);
						actionBar.setAnimStyle(QuickActionBar.ANIM_AUTO);
						actionBar.show();
					}
				});
	}

	public MainView getMainView() {
		return mainView;
	}

	public Gallery getGallery() {
		return gallery;
	}

	public CommandSender getCommandSender() {
		return commandSender;
	}

	public void setCommandSender(CommandSender commandSender) {
		this.commandSender = commandSender;
	}
}
